//
//  CollectionAssetViewCell.swift
//  JJImagePicker
//
//  Created by 李加建 on 2021/5/31.
//

import Photos
import UIKit

class AlbumCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var album: AlbumModel? {
        didSet {
            guard let album = self.album else { return }
            update(album: album)
        }
    }

    private lazy var imageView: UIImageView = {
        let imageView = UIImageView(frame: bounds)
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()

    private lazy var textLabel: UILabel = {
        let lazy = UILabel()
        return lazy
    }()

    private lazy var countLabel: UILabel = {
        let lazy = UILabel()
        lazy.textColor = .lightGray
        return lazy
    }()

    private func update(album: AlbumModel) {
        textLabel.text = album.name
        countLabel.text = String("(\(album.count))")
        asset = album.result?.firstObject

        textLabel.sizeToFit()
        countLabel.sizeToFit()

        textLabel.frame = CGRect(x: 68, y: 16, width: textLabel.frame.width, height: 24)
        countLabel.frame = CGRect(x: textLabel.frame.maxX + 8, y: 16, width: countLabel.frame.width, height: 24)
    }

    private var imageRequestID: PHImageRequestID?
    private var requestLocalIdentifier: String = ""

    var asset: PHAsset? {
        didSet {
            imageView.image = nil
            if let asset = asset {
                requestLocalIdentifier = asset.localIdentifier
                if let requestID = imageRequestID {
                    PHImageManager.default().cancelImageRequest(requestID)
                }

                let size = CGSize(width: 300, height: 300)
                let options = PHImageRequestOptions()
                options.isSynchronous = true
                PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: options) { [weak self] image, _ in
                    guard let self = self else { return }
                    if self.requestLocalIdentifier == asset.localIdentifier {
                        self.imageView.image = image
                    } else {
                        if let requestID = self.imageRequestID {
                            PHImageManager.default().cancelImageRequest(requestID)
                        }
                        self.imageRequestID = nil
                    }
                }
            }
        }
    }

    private func setup() {
        contentView.addSubview(imageView)
        contentView.addSubview(textLabel)
        contentView.addSubview(countLabel)
        textLabel.frame = bounds

        imageView.frame = CGRect(x: 0, y: 0, width: 56, height: 56)
        textLabel.frame = CGRect(x: 68, y: 16, width: 100, height: 24)
        countLabel.frame = CGRect(x: 168, y: 16, width: 100, height: 24)
    }
}
